package cn.demomaster.quickpicturer.config;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;

import androidx.fragment.app.Fragment;

import java.io.File;
import java.util.Random;

import cn.demomaster.quickpicturer.constants.ImageActionType;
import cn.demomaster.quickpicturer.picker.QuickPicturer;
import cn.demomaster.quickzxing.QuickCaptureActivity;

public class ImageActionRequest {
    public ImageActionType imageActionType;//请求类型
    public String authority;
    public float aspectX = 1;
    public float aspectY = 1;
    public int outputX = 800;
    public int outputY = 800;
    public Uri srcUri = null;//源文件存放uri
    public Uri outputUri = null;//输出文件存放位置
    public Uri croppedImageUri = null;//输出文件存放位置
    public String outputPath = null;//输出文件存放位置
    public File saveDirectory = null;//输出文件存放位置
    public int requestCode = 11111;
    public boolean isCroppingEnabled;
    public int maxImages=1;
    public Activity activity;
    public Fragment fragment;
    //OnImageSelectedListener onTakePhotoResult;
    public QuickPicturer.OnImageSelectedListener onImageSelectedListener;
    public Class<? extends Activity> scanActivityClass = QuickCaptureActivity.class;

    public ImageActionRequest(Activity activity) {
        this.activity = activity;
    }

    public ImageActionRequest(Fragment fragment) {
        this.fragment = fragment;
    }

    public ImageActionRequest(ImageConfig imageConfig, ImageActionType imageActionType) {
        this.outputUri = imageConfig.outputUri;
        this.activity = imageConfig.activity;
        this.fragment = imageConfig.fragment;
        this.maxImages = imageConfig.getMaxImages();
        this.imageActionType = imageActionType;
        this.saveDirectory = imageConfig.saveDirectory;
        this.authority = imageConfig.authority;

        Random random = new Random();
        int requestCode = 10000 + random.nextInt(90000);
        this.requestCode = requestCode;
    }

    public ImageActionRequest setImageActionType(ImageActionType imageActionType) {
        this.imageActionType = imageActionType;
        return this;
    }

    public ImageActionRequest setAspectX(float aspectX) {
        this.aspectX = aspectX;
        return this;
    }

    public ImageActionRequest setAspectY(float aspectY) {
        this.aspectY = aspectY;
        return this;
    }

    public ImageActionRequest setOutputX(int outputX) {
        this.outputX = outputX;
        return this;
    }

    public ImageActionRequest setOutputY(int outputY) {
        this.outputY = outputY;
        return this;
    }

    public ImageActionRequest setSrcUri(Uri srcUri) {
        this.srcUri = srcUri;
        return this;
    }

    public ImageActionRequest setOutputUri(Uri outputUri) {
        this.outputUri = outputUri;
        return this;
    }

    public ImageActionRequest setMaxImages(int maxImages) {
        this.maxImages = maxImages;
        return this;
    }

    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
    }

    public ImageActionRequest setRequestCode(int requestCode) {
        this.requestCode = requestCode;
        return this;
    }

//        public ImageRequestBuilder setOnTakePhotoResult(OnTakePhotoResult onTakePhotoResult) {
//            this.onTakePhotoResult = onTakePhotoResult;
//            return this;
//        }
//
//        public ImageRequestBuilder setOnSelectPictureResult(OnSelectPictureResult onSelectPictureResult) {
//            this.onSelectPictureResult = onSelectPictureResult;
//            return this;
//        }


    public ImageActionRequest setOnImageSelectedListener(QuickPicturer.OnImageSelectedListener onImageSelectedListener) {
        this.onImageSelectedListener = onImageSelectedListener;
        return this;
    }

    public ImageActionRequest setScanActivityClass(Class<? extends Activity> scanActivityClass) {
        this.scanActivityClass = scanActivityClass;
        return this;
    }

    public ImageActionRequest setCroppingEnabled(boolean croppingEnabled) {
        isCroppingEnabled = croppingEnabled;
        return this;
    }

    public Context getContext(){
        return fragment!=null?fragment.getContext():activity;
    }

    public Activity getActivity(){
        return fragment!=null?fragment.getActivity():activity;
    }


    public ImageActionRequest setAuthority(String authority) {
        this.authority = authority;
        return this;
    }

    public void resetRequestCode() {
        Random random = new Random();
        int requestCode = 10000 + random.nextInt(90000);
        this.requestCode = requestCode;
    }
}
